﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text.Json.Serialization;
using System.Windows.Data;
using System.Windows.Media;

using Xc.DataAccess.OneNetCore.Services;

namespace Xc.Logics.AepCoap.Models
{
    public class DeviceStateConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is NbDeviceInfo deviceInfoList)
            {
                switch (deviceInfoList.IsOnline)
                {
                    case true:
                        return Brushes.Green;
                    case false:
                        return Brushes.DarkGray;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class AddDeviceModel : INotifyPropertyChanged
    {
        /// <summary>
        /// 平台
        /// </summary>
        [JsonIgnore()]
        public Plantform Plantform
        {
            get
            {
                if (string.IsNullOrEmpty(IMSI))
                {
                    return Plantform.未知;
                }
                var text = IMSI.Substring(3, 2);
                if (int.TryParse(text, out var n))
                {
                    Plantform plantform = new Plantform();
                    switch (n)
                    {
                        case 3:
                        case 5:
                        case 11: plantform = Plantform.电信; break;

                        case 0:
                        case 2:
                        case 4:
                        case 7:
                            plantform = Plantform.移动; break;
                        case 1:
                        case 6:
                        case 9:
                            plantform = Plantform.联通; break;
                        case 20:
                            plantform = Plantform.铁通; break;
                        default:
                            break;
                    }
                    return plantform;
                }
                else
                {
                    return Plantform.未知;
                }
            }
        }
        private string _AddDeviceStatus { get; set; }
        public string DeviceNumber { get; set; }
        public string IMSI { get; set; }
        public string IMEI { get; set; }
        public string ICCID { get; set; }
        public string ProductTime { get; set; }

        public string AddDeviceStatus
        {
            get
            {
                return _AddDeviceStatus;
            }
            set
            {

                _AddDeviceStatus = value;
                Changed();
            }
        }

        public DateTime CreateTime
        {
            get
            {
                if (string.IsNullOrEmpty(ProductTime))
                {
                    return DateTime.MinValue;
                }
                else
                {
                    if (DateTime.TryParse(ProductTime, out var dt))
                    {
                        return dt;
                    }
                    else
                    {
                        return DateTime.MinValue;
                    }
                }

            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Changed(string name = null)
        {

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}
